use clap::*;

include!("switch-parser.rs");

fn main() {    
    let matches = App::new("Simple Config Switch")
        .version("1.0")
        .arg(Arg::with_name("config")
            .short("c").long("config")
            .help("set config file path")
            .required(true)
            .takes_value(true))
        .arg(Arg::with_name("switch-to")
            .short("s").long("switch-to")
            .takes_value(true)
            .help("switch config to profile"))
        .arg(Arg::with_name("list")
            .short("l").long("list")
            .help("list profile"))
        .arg(Arg::with_name("dry-run")
            .short("n").long("dry-run")
            .help("do not update any file, just print action"))
        .get_matches();
    
    let config = matches.value_of("config");
    if config.is_none() {
        return;
    }
    
    let switch_to = matches.value_of("switch-to");
    let is_list = matches.is_present("list");
    let is_dry_run = matches.is_present("dry-run");
    
    if !is_list && switch_to.is_none() {
        return;
    }
     
    let mut parser = SwitchParser::new();
    parser.load_from_yaml(config.unwrap());
        
    if is_list {
        // print profiles
        let profile_list = parser.enum_profile();
        
        for (profile, desc) in profile_list {
            println!("Profile: {}", profile);
            
            match desc {
                Some(v) => println!("\t{}", v),
                None => continue,
            }
        }
    } else if let Some(profile) = switch_to {
        // switch config to profile
        parser.switch_to(profile, is_dry_run);
    } else {
        println!("command is required for action");
    }
}
